package com.qf.work20250111;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @author zdl
 * @date 2025/1/11
 */
public class Demo3 {
    public static void main(String[] args) throws NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException {
        // 反射  获取 方法
        Class<Person> pc = Person.class;
        //获取 eat() 方法
        Method method1 = pc.getDeclaredMethod("eat", null);
        method1.invoke(pc.newInstance(),null);
        //获取eat(String name) 方法
        Method method2 = pc.getMethod("eat", String.class);
        method2.invoke(pc.newInstance(),"张三");
        //获取eat(String name,int age)方法
        Method method3 = pc.getDeclaredMethod("eat", String.class, int.class);
        method3.invoke(pc.newInstance(),"Tom",22);
    }
}
